package com.itheima.web.controller;

import com.itheima.domain.db.Freeze;
import com.itheima.domain.db.Unfreeze;
import com.itheima.web.manager.UserManager;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;

    //查询用户列表
    @GetMapping("/manage/users")
    public ResponseEntity findByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){

        return userManager.findByPage(pageNum,pageSize);
    }

    //查询用户基本信息
    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findUserInfoById(@PathVariable Long userId){
        //1.接受路径参数
        //2.调用manager完成查询
        return userManager.findUserInfoById(userId);
    }


    //查询用户的动态
    @GetMapping("/manage/messages")
    public ResponseEntity findMovementByConditionAndPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            @RequestParam(value = "uid",required = false)Long userId,
            @RequestParam(required = false)Integer state){
        return userManager.findMovementByConditionAndPage(userId,state,pageNum,pageSize);
    }

    //查询动态详情
    @GetMapping("/manage/messages/{publishId}")
    public ResponseEntity findMovementById(@PathVariable ObjectId publishId){
        return userManager.findMovementById(publishId);
    }

    @GetMapping("/manage/messages/comments")
    public ResponseEntity findMovementComemntByPage(
            @RequestParam(value = "page",defaultValue = "1",required = false) Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10",required = false) Integer pageSize,
            @RequestParam(value = "messageID") ObjectId publishId){
        return userManager.findMovementComemntByPage(pageNum,pageSize,publishId);
    }

    //冻结
    @PostMapping("/manage/users/freeze")
    public ResponseEntity freezeUser(@RequestBody Freeze freeze){

        return userManager.freezeUser(freeze);
    }

    //解冻
    @PostMapping("/manage/users/unfreeze")
    public ResponseEntity unfreezeUser(@RequestBody Freeze freeze){
        return userManager.unfreezeUser(freeze);
    }

}
